<?php
/**
 * 分析API模块脚本
 * 用于分析api_mapping.json中的模块和接口信息
 */

// 读取API映射文件
$apiMapping = json_decode(file_get_contents('api_mapping.json'), true);

echo "========================================\n";
echo "API模块分析报告\n";
echo "========================================\n\n";

// 统计各模块接口数量
$moduleStats = [];
$totalEndpoints = 0;

foreach ($apiMapping as $module => $moduleInfo) {
    $endpointCount = count($moduleInfo['endpoints']);
    $moduleStats[$module] = [
        'name' => $moduleInfo['name'],
        'count' => $endpointCount
    ];
    $totalEndpoints += $endpointCount;
}

// 按接口数量排序
uasort($moduleStats, function($a, $b) {
    return $b['count'] - $a['count'];
});

echo "模块统计:\n";
echo sprintf("%-15s %-20s %s\n", "模块标识", "模块名称", "接口数量");
echo str_repeat("-", 50) . "\n";

foreach ($moduleStats as $module => $info) {
    echo sprintf("%-15s %-20s %d\n", $module, $info['name'], $info['count']);
}

echo str_repeat("-", 50) . "\n";
echo sprintf("%-15s %-20s %d\n", "总计", "", $totalEndpoints);
echo "\n";

// 显示前几个模块的详细接口信息
echo "详细接口信息 (前3个模块):\n";
echo "========================================\n";

$moduleCount = 0;
foreach ($moduleStats as $module => $info) {
    if ($moduleCount >= 3) break;
    
    echo "\n模块: {$info['name']} ({$module}) - {$info['count']} 个接口\n";
    echo str_repeat("-", 40) . "\n";
    
    // 显示该模块的前5个接口
    $endpoints = $apiMapping[$module]['endpoints'];
    $endpointCount = 0;
    foreach ($endpoints as $endpoint) {
        if ($endpointCount >= 5) break;
        
        echo "  {$endpoint['http_method']} {$endpoint['src']} - {$endpoint['title']} {$endpoint['name']}\n";
        $endpointCount++;
    }
    
    if (count($endpoints) > 5) {
        echo "  ... 还有 " . (count($endpoints) - 5) . " 个接口\n";
    }
    
    $moduleCount++;
}

echo "\n建议的改造顺序:\n";
echo "1. 优先改造核心业务模块 (如 customer, contract, finance)\n";
echo "2. 然后改造系统管理模块 (如 home, adm)\n";
echo "3. 最后改造辅助功能模块 (如 disk, office)\n";